﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jungle.Code.Utils;
using System.Text.RegularExpressions;

using Jungle.Code.DAL;

namespace Jungle.Articles
{
    public class Article
    {
        public string err;
        public DataSet NewsDataSet;
        public List<Article> NewsLisr;
        
        private int newId;
        private string title;
        private string description;
        private DateTime pubDate;
        private string img;
        private int languageId;
        
        public int NewId { get { return newId; } set { newId = value; } }
        public string Title { get { return title; } set { title = value; } }
        public string Description { get { return description; } set { description = value; } }
        public DateTime PubDate { get { return pubDate; } set { pubDate = value; } }
        public string Img { get { return img; } set { img = value; } }
        public int LanguageId { get { return languageId; } set { languageId = value; } }

        // -----------------------------------------------------------------------------
        public void Add()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand("articles.mdNews_Add", connect);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@title", SqlDbType.NChar, 255).Value = title;
                    command.Parameters.Add("@body", SqlDbType.Text).Value = description;
                    command.Parameters.Add("@img", SqlDbType.VarChar).Value = img;
                    command.Parameters.Add("@languageId", SqlDbType.Int).Value = languageId;
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
                catch (SqlException ex)
                {
                    err = ex.ToString();
                }
                finally
                {
                    connect.Close();
                }
            }        
        }
        // -----------------------------------------------------------------------------
        public void Update()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand("articles.mdNews_Update", connect);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@id_news", SqlDbType.Int).Value = newId;
                    command.Parameters.Add("@title", SqlDbType.NChar, 255).Value = title;
                    command.Parameters.Add("@body", SqlDbType.Text).Value = description;
                    command.Parameters.Add("@img", SqlDbType.VarChar).Value = img;
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
                catch (SqlException ex)
                {
                    err = ex.ToString();
                }
                finally
                {
                    connect.Close();
                }
            }
        }
        // -----------------------------------------------------------------------------
        public void Delete()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand("articles.mdNews_Del", connect);
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@pk", SqlDbType.Int).Value = newId;
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
                }
                catch (SqlException ex)
                {
                    err = ex.ToString();
                }
                finally
                {
                    connect.Close();
                }
            }
        }
        // -----------------------------------------------------------------------------
        public void GetById()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand("articles.mdNews_GetById", connect);
                    SqlDataReader reader = null;
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add("@NewId", SqlDbType.Int).Value = newId;
                    command.Connection.Open();
                    reader = command.ExecuteReader();
                    if (reader.Read())
                    { 
                        newId = (int)reader["id_news"];
                        title = reader["title"].ToString().Trim();
                        description = reader["body"].ToString().Trim();
                        pubDate = (DateTime)reader["pubDate"];
                        img = reader["Img"].ToString();
                        languageId = (int)reader["languageId"];
                    }
                    reader.Close();
                    command.Connection.Close();
                }
                catch (SqlException ex)
                {
                    err = ex.ToString();
                }
                finally
                {
                    connect.Close();
                }
            }
        }
        // -----------------------------------------------------------------------------
        public void GetAllInList()
        {
            GetAll(0);
        }
        // -----------------------------------------------------------------------------
        public void GetAllInDS()
        {
            GetAll(1);
        }
        // -----------------------------------------------------------------------------
        private void GetAll(int by)
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand("articles.mdNews_GetAll2", connect);
                    if (by == 0)
                    {
                        SqlDataReader reader = null;
                        command.CommandType = CommandType.StoredProcedure;

                        NewsLisr = new List<Article>();
                        command.Connection.Open();
                        reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            Article n = new Article();
                            n.newId = (int)reader["id_news"];
                            n.title = reader["title"].ToString().Trim();
                            n.description = reader["body"].ToString().Trim();
                            n.pubDate = (DateTime)reader["pubDate"];
                            n.img = reader["Img"].ToString();
                            n.languageId = (int)reader["languageId"];

                            if(n.LanguageId == languageId)
                            NewsLisr.Add(n);
                        }
                        reader.Close();
                        command.Connection.Close();
                    }
                    if (by == 1)
                    {
                        NewsDataSet = new DataSet();
                        NewsDataSet.Clear();
                        SqlDataAdapter myData = new SqlDataAdapter("select * from articles.mdNews where [languageId]=" + languageId.ToString() + " order by [pubDate] desc;", connect);
                        myData.Fill(NewsDataSet);                        
                    }
                }
                catch (SqlException ex)
                {
                    err = ex.ToString();
                }
                finally
                {
                    connect.Close();
                }
            }
        }
        // -----------------------------------------------------------------------------
        public void GenereateRss()
        {
            string body = "";
            body = "<?xml version='1.0' encoding='windows-1251'?>\r\n";
            body += "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:content='http://purl.org/rss/1.0/modules/content/'><channel><title>Pokerminds.net лента новостей</title><link>" + UrlHelper.GetSiteUrl().ToString() + "</link><atom:link href='" + UrlHelper.GetSiteUrl().ToString() + "/UserFiles/RSS/news.xml' rel='self' type='application/rss+xml' /><description>Лента новостей клуба Pokerminds.net</description><language>ru</language>";

            Article no = new Article();
            no.GetAll(0);
            foreach (Article n in no.NewsLisr)
            {
                n.description = Regex.Replace(n.Description, "<[^>]*?>", string.Empty, RegexOptions.IgnoreCase);
                if (n.description.Length > 1)
                {
                    body += "<item>\r\n";
                    body += "<link>" + UrlHelper.GetSiteUrl() + "/ru/new.aspx?i=" + n.newId.ToString() + "</link>\r\n";
                    body += "<title>" + n.title + "</title>\r\n";
                    body += "<pubDate>" + n.pubDate + "</pubDate>\r\n";
                    body += "<description><![CDATA[" + n.PubDate.ToString() + "<br/><br/>" + n.Description + "]]></description>\r\n";
                    body += "</item>\r\n";
                }
            }
            body += "</channel>\r\n</rss>";
            System.IO.StreamWriter writeXml = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath("/UserFiles/RSS/news.xml"), false, System.Text.Encoding.GetEncoding(1251));
            writeXml.Write(body);
            writeXml.Close();
        }
        // -----------------------------------------------------------------------------
    }
}
